
//
// 101101 - AH - Created.
// 101125 - AH - Changed from NetworkChangeEvent and NetworkChangeListener to
//               SoekratChangeEvent and SoekratChangeListener.
// 101217 - AH - Moved from org.soekrat.swing
// 110606 - AH - (Garage) Changes related to that NetworkNode now has 
//               SoekratName.
// 110608 - AH - (Bybanen *) Implemented ListCellRenderer so gets SoekratName 
//               string presentations for nodes in cluster list.
// 120321 - AH - Removed @Override for interface method (1.5 compatible).
//

package org.soekrat.swing.network;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;

import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;

import org.soekrat.engine.event.SoekratChangeEvent;
import org.soekrat.engine.event.SoekratChangeListener;
import org.soekrat.engine.network.Cluster;
import org.soekrat.engine.network.Network;

/**
 * <p>
 *   View that simple shows a 
 *   {@link Network}'s
 *   {@link Cluster}s in a
 *   {@link JList} view.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
@SuppressWarnings("serial")
public final class ClusterListView extends JPanel 
{
  private final DefaultListModel m_model=new DefaultListModel();
  
  private final JList m_list=new JList(m_model);
  
  private Network m_network=null;
  
  private final static ListCellRenderer c_renderer=new ListCellRenderer()
  {
    private final DefaultListCellRenderer m_dlcr=new DefaultListCellRenderer();
    
    // Specified in ListCellRenderer.
    public Component getListCellRendererComponent(JList list, Object val,
      int idx, boolean selected, boolean focus) 
    {
      String txt=(val==null) ? "" : val.toString();
      if (val instanceof Cluster)
      {
        txt=((Cluster)val).toStringSoekratNames();
      }
      return m_dlcr.getListCellRendererComponent(list, txt, idx, selected, 
        focus);
    }
    
  };
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public ClusterListView()
  {
    setLayout(new BorderLayout());
    JPanel title=new JPanel(new FlowLayout(FlowLayout.CENTER));
    title.add(new JLabel("Clusters"));
    add(title, BorderLayout.NORTH);
    m_list.setCellRenderer(c_renderer);
    add(new JScrollPane(m_list), BorderLayout.CENTER);
  }
  
  /**
   * <p>
   *   Sets
   *   {@link Network} to display
   *   {@link Cluster}s for.
   * </p>
   * @param network {@link Network} or {@code null} if to clear.
   */
  public void setNetwork(Network network)
  {
    if (network!=m_network)
    {
      if (m_network!=null) m_network.removeSoekratChangeListener(m_ncl);
      m_network=network;
      m_network.removeSoekratChangeListener(m_ncl);
      update();
    }
  }
  
  /**
   * <p>
   *   Updates view.
   * </p>
   */
  public void update()
  { 
    m_model.removeAllElements();
    if (m_network!=null)
    {  
      int n=m_network.getClusterCount();
      for (int i=0; i<n; i++) 
      {
        m_model.addElement(m_network.getCluster(i));
      }
    }
  }
  
  private final SoekratChangeListener m_ncl=new SoekratChangeListener()
  {
    public void changed(SoekratChangeEvent e){ update(); }
  };
  
}
